package com.dkt.api.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.dkt.api.domain.UserMapper;
import com.dkt.api.web.dto.UserDto;
import com.dkt.base.StrUtil;
import com.dkt.base.domain.User;

@Service
public class UserSrvImpl implements UserSrv {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	public void userReg(UserDto userDto) {
		User user = new User();
		user.setId(StrUtil.genUUID());
		user.setUsername(userDto.getUsername());
		user.setPassword(passwordEncoder.encode(userDto.getPassword()));
		userMapper.insUserReg(user);
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userMapper.selUserByUsername(username);
		if (user == null) {
			throw new UsernameNotFoundException("username cannot be found!");
		}
		return user;
	}

}
